import 'babel-polyfill'; // vuex 兼容ie，后面可能也有兼容
import VueClipboard from 'vue-clipboard2';
import ElementUI from 'element-ui'; // 引入element-ui
import { Base64 } from 'js-base64';
import 'element-ui/lib/theme-chalk/index.css'; // 引入element-ui的css库
import 'element-ui/lib/theme-chalk/display.css';
import Vue from 'vue';
import App from './App';
import router from './router';
import store from './store'; // 引入vuex
import $api from './api'; // 配置api
import { $C, $rp, $i } from './utils/constants';
import * as $va from './utils/validators';
import backtop from './utils/backtop';

import SvgIcon from './components/shared/SvgIcon';
import Timeago from './components/shared/Timeago';
import TimeagoV2 from './components/shared/Timeago-v2';
import RequireSignInTooltip from './components/shared/RequireSignInTooltip';
import Pagination from './components/shared/Pagination';
import Breadcrumb from './components/shared/Breadcrumb';
import CKEditor from './components/shared/CKEditor';
import TinymceEditor from './components/tinymce-editor';
import Emoji from './components/emoji';

/* 配置svg */
const requireAll = requireContext => requireContext.keys().map(requireContext);
const req = require.context('./assets/icons', false, /\.svg$/); // 访问到icons路径
requireAll(req);

Vue.config.productionTip = false;
Vue.use(ElementUI);
Vue.use(VueClipboard);
Vue.component('svg-icon', SvgIcon);
Vue.component('timeago', Timeago);
Vue.component('timeago2', TimeagoV2);
Vue.component('sign-in-tooltip', RequireSignInTooltip);
Vue.component('pagination', Pagination);
Vue.component('breadcrumb', Breadcrumb);
Vue.component('ckeditor', CKEditor);
Vue.component('tinymce-editor', TinymceEditor);
Vue.component('emoji', Emoji);

const goBack = () => {
  if (window.history.length > 1) router.go(-1);
  else router.push('/').catch(() => {});
};

const goSignIn = () => {
  const signPath = `/${$rp.USERS}/${$rp.SIGN_IN}`;
  const redirectPath = router.currentRoute.fullPath;
  router.push({ path: signPath, query: { redirect: redirectPath } }).catch(() => {});
};

const reloadQuery = (query = {}) => router.push({ path: router.path, query }).catch(() => {});

const addQuery = (adds = {}) => {
  const query = Object.assign({}, router.history.current.query, adds);
  reloadQuery(query).catch(() => {});
};

const stopPropagation = (e) => { e.stopPropagation(); };

const isNum = val => /^\d+$/.test(val) || /^[0-9]+$/.test(val);

Object.assign(Vue.prototype, {
  $api,
  $rp,
  $C,
  $i,
  $va,
  Base64,
  goBack,
  reloadQuery,
  addQuery,
  goSignIn,
  stopPropagation,
  backtop,
  isNum,
});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App),
});
